package org.aphelie.torg.ruler.internal.core;

import org.aphelie.torg.ruler.core.IModelProviderEvent;
import org.aphelie.torg.ruler.core.IModelProviderListener;
import org.eclipse.core.runtime.ListenerList;

public abstract class AbstractModelManager {

    private ListenerList fListeners = new ListenerList();

    public synchronized void removeModelProviderListener(IModelProviderListener listener) {
        // TODO see bug 214613... investigate why FeatureModelManager is being bad
        if (listener != null) {
            fListeners.remove(listener);
        }
    }

    public synchronized void addModelProviderListener(IModelProviderListener listener) {
        fListeners.add(listener);
    }

    public void fireModelProviderEvent(IModelProviderEvent event) {
        Object[] listeners = fListeners.getListeners();
        for (int i = 0; i < listeners.length; i++) {
            ((IModelProviderListener) listeners[i]).modelsChanged(event);
        }
    }

    public void shutdown() {
        removeListeners();
    }

    protected void removeListeners() {
        fListeners.clear();
    }

}
